<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学院名称" prop="name">
        <el-autocomplete
          :fetch-suggestions="querySearch"
          placeholder="请输入学院名称"
          v-model="queryParams.name"
          trigger-on-focus="false"></el-autocomplete>
      </el-form-item>
      <el-form-item label="所在省市" prop="location">
        <template>
          <el-select v-model="queryParams.location" placeholder="请选择省份">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:college:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学院id" align="center" prop="collegeId" />
      <el-table-column label="学院名称" align="center" prop="name" />
      <el-table-column label="所在省市" align="center" prop="location" />
      <el-table-column label="男女比例" align="center" prop="malefemale" />
      <el-table-column label="就业率" align="center" prop="rate"/>
      <el-table-column label="校友会排名" align="center" prop="alumniassociationrankings"/>
      <el-table-column label="软科排名" align="center" prop="softseccoranking"/>
      <el-table-column label="考研率" align="center" prop="kaoyanlv"/>
      <el-table-column label="办学性质" align="center" prop="baoyanlv"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:college:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:college:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学院名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入学院名称" />
        </el-form-item>
        <el-form-item label="所在省市" prop="location">
          <el-input v-model="form.location" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="男女比例" prop="malefemale">
          <el-input v-model="form.malefemale" placeholder="请输入男女比例" />
        </el-form-item>
        <el-form-item label="就业率" prop="rate">
          <el-input v-model="form.rate" placeholder="请输入就业率" />
        </el-form-item>
        <el-form-item label="校友会排名" prop="alumniassociationrankings">
          <el-input v-model="form.alumniassociationrankings" placeholder="请输入校友会排名" />
        </el-form-item>
        <el-form-item label="软科排名" prop="softseccoranking">
          <el-input v-model="form.softseccoranking" placeholder="请输入软科排名" />
        </el-form-item>
        <el-form-item label="考研率" prop="kaoyanlv">
          <el-input v-model="form.kaoyanlv" placeholder="请输入考研率" />
        </el-form-item>
        <el-form-item label="办学性质" prop="baoyanlv">
          <el-input v-model="form.baoyanlv" placeholder="请输入保研率" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPost, getPost, delPost, addPost, updatePost,collegeFind} from "@/api/system/college";

export default {
  name: "college",
  data() {
    return {
      options: [{
        value: '山东济南',
        label: '山东济南'
      }, {
        value: '北京市',
        label: '北京市'
      }, {
        value: '天津市',
        label: '天津市'
      }, {
        value: '贵州省',
        label: '贵州省'
      }, {
        value: '上海市',
        label: '上海市'
      }, {
        value: '重庆市',
        label: '重庆市'
      }, {
        value: '河北省',
        label: '河北省'
      }, {
        value: '内蒙古自治区',
        label: '内蒙古自治区'
      }, {
        value: '辽宁省',
        label: '辽宁省'
      }, {
        value: '吉林省',
        label: '吉林省'
      }, {
        value: '黑龙江省',
        label: '黑龙江省'
      }, {
        value: '江苏省',
        label: '江苏省'
      }, {
        value: '安徽省',
        label: '安徽省'
      }, {
        value: '福建省',
        label: '福建省'
      }, {
        value: '江西省',
        label: '江西省'
      }, {
        value: '河南省',
        label: '河南省'
      }, {
        value: '湖北省',
        label: '湖北省'
      }, {
        value: '湖南省',
        label: '湖南省'
      }, {
        value: '广东省',
        label: '广东省'
      }, {
        value: '海南省',
        label: '海南省'
      }, {
        value: '广西壮族自治区',
        label: '广西壮族自治区'
      }, {
        value: '四川省',
        label: '四川省'
      }, {
        value: '云南省',
        label: '云南省'
      }, {
        value: '西藏自治区',
        label: '西藏自治区'
      }, {
        value: '陕西省',
        label: '陕西省'
      }, {
        value: '甘肃省',
        label: '甘肃省'
      }, {
        value: '宁夏回族自治区',
        label: '宁夏回族自治区'
      }, {
        value: '青海省',
        label: '青海省'
      }, {
        value: '新疆维吾尔族自治区',
        label: '新疆维吾尔族自治区'
      }, {
        value: '香港特别行政区',
        label: '香港特别行政区'
      }, {
        value: '澳门特别行政区',
        label: '澳门特别行政区'
      }, {
        value: '台湾省',
        label: '台湾省'
      }],
      value: '',
      inputValue:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        location: undefined,
        malefemale: undefined,
        rate: undefined,
        alumniassociationrankings: undefined,
        softseccoranking: undefined,
        kaoyanlv: undefined,
        baoyanlv: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "学院名称不能为空", trigger: "blur" }
        ],
        location: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        malefemale: [
          { required: true, message: "男女比例不能为空", trigger: "blur" }
        ],
        rate: [
          { required: true, message: "就业率不能为空", trigger: "blur" }
        ],
        alumniassociationrankings: [
          { required: true, message: "校友会排名不能为空", trigger: "blur" }
        ],
        softseccoranking: [
          { required: true, message: "软科排名不能为空", trigger: "blur" }
        ],
        kaoyanlv: [
          { required: true, message: "考研率不能为空", trigger: "blur" }
        ],
        baoyanlv: [
          { required: true, message: "办学性质不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    querySearch(queryString, cb){
      console.log(queryString)
      if (queryString==""){
        queryString=1
      }
      collegeFind(queryString).then(response => {
        console.log(response.rows)
        this.valueList=response.rows
        let s=this.valueList.map(item =>{
          let res={};
          res.value=item.name;
          return res;
        })
        console.log(s)
        cb(s)
      })
    },
    getList() {
      this.loading = true;
      listPost(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        collegeId: undefined,
        name: undefined,
        location: undefined,
        malefemale: undefined,
        rate: undefined,
        alumniassociationrankings: undefined,
        softseccoranking: undefined,
        kaoyanlv: undefined,
        baoyanlv: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.collegeId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学院";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const collegeId = row.collegeId || this.ids
      getPost(collegeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学院信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.collegeId != undefined) {
            updatePost(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPost(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const collegeId = row.collegeId || this.ids;
      this.$modal.confirm('是否确认删除学院编号为"' + collegeId + '"的数据项？').then(function() {
        return delPost(collegeId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
